package com.shuhua.taskServer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuhua.common.exception.BusinessException;
import com.shuhua.common.result.Result;
import com.shuhua.pojo.dto.CraftsmanAuthDTO;
import com.shuhua.pojo.dto.CraftsmanUpdateDTO;
import com.shuhua.pojo.entity.Craftsman;
import com.shuhua.pojo.vo.CraftsmanVO;
import com.shuhua.pojo.vo.UserInfo;
import com.shuhua.taskServer.mapper.CraftsmanMapper;
import com.shuhua.taskServer.service.CraftsmanService;
import com.shuhua.taskServer.service.security.GenericUserDetails;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * 工匠服务实现类 (合并版本)
 */
@Service
public class CraftsmanServiceImpl extends ServiceImpl<CraftsmanMapper, Craftsman> implements CraftsmanService {

    // =================================================================
    // =========== 以下是您个人信息模块需要添加的逻辑和方法 ============
    // =================================================================

    /**
     * [新增] 从安全上下文中获取当前登录工匠的ID
     */
    private Long getCurrentCraftsmanId() {
        try {
            Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            GenericUserDetails<?> userDetails = (GenericUserDetails<?>) principal;
            UserInfo userInfo = userDetails.getUser();
            return userInfo.getId();
        } catch (Exception e) {
            throw new BusinessException(401, "用户未登录或认证信息无效");
        }
    }

    /**
     * [新增] 获取当前登录工匠的个人信息
     */
    @Override
    public CraftsmanVO getInfo() {
        Long craftsmanId = getCurrentCraftsmanId();
        Craftsman craftsman = getById(craftsmanId); // 使用 ServiceImpl 自带的 getById
        if (craftsman == null) {
            throw new BusinessException(404, "用户不存在");
        }
        CraftsmanVO vo = new CraftsmanVO();
        BeanUtils.copyProperties(craftsman, vo);
        return vo;
    }

    /**
     * [新增] 更新当前登录工匠的个人信息
     */
    @Override
    public void updateInfo(CraftsmanUpdateDTO updateDTO) {
        Long craftsmanId = getCurrentCraftsmanId();
        Craftsman craftsman = getById(craftsmanId);
        if (craftsman == null) {
            throw new BusinessException(404, "用户不存在");
        }

        BeanUtils.copyProperties(updateDTO, craftsman);
        updateById(craftsman); // 使用 ServiceImpl 自带的 updateById
    }

    /**
     * [新增] 提交当前登录工匠的认证信息
     */
    @Override
    public void submitAuthentication(CraftsmanAuthDTO authDTO) {
        Long craftsmanId = getCurrentCraftsmanId();

        if (!StringUtils.hasText(authDTO.getIdCardFrontUrl()) || !StringUtils.hasText(authDTO.getIdCardBackUrl())) {
            throw new BusinessException(400, "请上传身份证正反面照片");
        }

        Craftsman craftsman = getById(craftsmanId);
        if (craftsman == null) {
            throw new BusinessException(404, "用户不存在");
        }

        craftsman.setIdCardFront(authDTO.getIdCardFrontUrl());
        craftsman.setIdCardBack(authDTO.getIdCardBackUrl());
        craftsman.setAuthStatus(2);
        craftsman.setAuthTime(new Date());

        updateById(craftsman);
    }


    @Override
    public Result<Craftsman> getCraftsmanInfo(Long craftsmanId) {
        Craftsman craftsman = getById(craftsmanId);
        if (craftsman == null) {
            return Result.error("工匠不存在");
        }
        return Result.success(craftsman);
    }

    @Override
    public Result<Boolean> updateToTeamLeader(Long craftsmanId) {
        Craftsman craftsman = getById(craftsmanId);
        if (craftsman == null) {
            return Result.error("工匠不存在");
        }

        craftsman.setIsTeamLeader(1);
        boolean updated = updateById(craftsman);

        if (updated) {
            return Result.success("更新成功", true);
        } else {
            return Result.error("更新失败");
        }
    }
}